#Boa:Frame:MathFrame

#### MATH FRAME ####

#Immediate Needs:
#   1) Add clickable text (or maybe buttons) that add functions to the
#      MainFrames's TxtQuestion box that don't appear on the MainFrame
#      itself (e.g. Newton, defDeriv, abs, etc.)
#   2) Refine the way the buttons retrieve user information and deal with it.
#      e.g. OnBtnQdrtcClick needs to use a form with three text fields rather
#      than three MessageBoxes.  This coding style is the backwash from my VB
#      class.

import wx
import QdrtcImgnryFrame
import PiMath

IsRunning = False

def create(parent):
    global IsRunning
    IsRunning = True
    return MathFrame(parent)

[wxID_MATHFRAME, wxID_MATHFRAMEBTNGCF, wxID_MATHFRAMEBTNISPRIME, 
 wxID_MATHFRAMEBTNLCD, wxID_MATHFRAMEBTNQDRTC, wxID_MATHFRAMEBTNREDRAD, 
] = [wx.NewId() for _init_ctrls in range(6)]

class MathFrame(wx.MiniFrame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_MATHFRAME, name='MathFrame',
              parent=prnt, pos=wx.Point(410, 342), size=wx.Size(407, 289),
              style=wx.DEFAULT_FRAME_STYLE, title='Math')
        self.SetClientSize(wx.Size(399, 255))
        self.SetBackgroundColour(wx.Colour(228, 228, 228))
        self.SetToolTipString('')

        self.BtnQdrtc = wx.Button(id=wxID_MATHFRAMEBTNQDRTC, label='Quadratic',
              name='BtnQdrtc', parent=self, pos=wx.Point(32, 16),
              size=wx.Size(60, 30), style=0)
        self.BtnQdrtc.SetToolTipString('')
        self.BtnQdrtc.Bind(wx.EVT_BUTTON, self.OnBtnQdrtcClick,
              id=wxID_MATHFRAMEBTNQDRTC)

        self.BtnGCF = wx.Button(id=wxID_MATHFRAMEBTNGCF, label='GCF',
              name='BtnGCF', parent=self, pos=wx.Point(95, 16), size=wx.Size(60,
              30), style=0)
        self.BtnGCF.SetToolTipString('')
        self.BtnGCF.Bind(wx.EVT_BUTTON, self.OnBtnGCFClick,
              id=wxID_MATHFRAMEBTNGCF)

        self.BtnLCD = wx.Button(id=wxID_MATHFRAMEBTNLCD, label='LCD',
              name='BtnLCD', parent=self, pos=wx.Point(158, 16),
              size=wx.Size(60, 30), style=0)
        self.BtnLCD.SetToolTipString('')
        self.BtnLCD.Bind(wx.EVT_BUTTON, self.OnBtnLCDClick,
              id=wxID_MATHFRAMEBTNLCD)

        self.BtnIsPrime = wx.Button(id=wxID_MATHFRAMEBTNISPRIME,
              label='Is Prime?', name='BtnIsPrime', parent=self,
              pos=wx.Point(221, 16), size=wx.Size(60, 30), style=0)
        self.BtnIsPrime.SetToolTipString('')
        self.BtnIsPrime.Bind(wx.EVT_BUTTON, self.OnBtnIsPrimeClick,
              id=wxID_MATHFRAMEBTNISPRIME)

        self.BtnRedRad = wx.Button(id=wxID_MATHFRAMEBTNREDRAD,
              label='Reduce Radical', name='BtnRedRad', parent=self,
              pos=wx.Point(284, 16), size=wx.Size(80, 30), style=0)
        self.BtnRedRad.SetToolTipString('')
        self.BtnRedRad.Bind(wx.EVT_BUTTON, self.OnBtnRedRadClick,
              id=wxID_MATHFRAMEBTNREDRAD)

        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def __init__(self, parent):
        self._init_ctrls(parent)

#Button Actions
#=====================================================================================================
    def OnCloseWindow(self, event):
        global IsRunning
        IsRunning = False
        self.Destroy()

    def OnBtnQdrtcClick(self, event):
        #try:
            A = wx.GetTextFromUser("Type the value of A", "Quadratic Formula")
            if A == "": wx.MessageBox("Action Canceled", "Quadratic Formula")
            elif float(PiMath.Evaluate(A)) == 0.0:
                wx.MessageBox("The value of A cannot be zero", "Quadratic Formula")
            else:
                A = PiMath.Evaluate(A)
                B = wx.GetTextFromUser("Type the value of B", "Quadratic Formula")
                if B == "": wx.MessageBox("Action Canceled", "Quadratic Formula")
                else:
                    B = PiMath.Evaluate(B)
                    C = wx.GetTextFromUser("Type the value of C", "Quadratic Formula")
                    if C == "": wx.MessageBox("Action Canceled", "Quadratic Formula")
                    else:
                        C = PiMath.Evaluate(C)
                        X = PiMath.Quadratic(float(A), float(B), float(C))
                        if X[0] == False:
                            if X[1] != X[2]:
                                wx.MessageBox("For " + str(A) + "x^2+" + str(B) + "x+" + 
                                              str(C) + ",\nx={" + str(X[1]) + ", " + str(X[2]) + "}",
                                              "Quadratic Formula")
                            else: wx.MessageBox("For " + str(A) + "x^2+" + str(B) + "x+" + 
                                               str(C) + ",\nx=" + str(X[1]))
                        else:
                            self.QuadImgFrame = QdrtcImgnryFrame.create(self)
                            self.QuadImgFrame.TxtNumerator.SetLabel(X[1])
                            self.QuadImgFrame.TxtDenominator.SetLabel(X[2])
                            self.QuadImgFrame.Show()
        #except:
        #    wx.MessageBox("Your Equation Could Not Be Calculated", "Quadratic Formula")

    def OnBtnGCFClick(self, event):
        try:
            A = wx.GetTextFromUser("Enter the first number.", "Greatest Common Factor")
            if A == "": wx.MessageBox("Action Canceled", "Greatest Common Factor")
            elif float(int(A)) != float(A):
                wx.MessageBox("The number must be an integer", "Greatest Common Factor")
            else:
                B = wx.GetTextFromUser("Enter the second number.", "Greatest Common Factor")
                if B == "": wx.MessageBox("Action Canceled", "Greatest Common Factor")
                elif float(int(B)) != float(B):
                    wx.MessageBox("The number must be an integer", "Greatest Common Factor")
                else:
                    X = PiMath.GCF(int(A), int(B))
                    wx.MessageBox(str(X), "Greatest Common Factor")
        except:
            wx.MessageBox("You must enter a number", "Greatest Common Factor")

    def OnBtnLCDClick(self, event):
        try:
            A = wx.GetTextFromUser("Enter the first number.", "Least Common Denominator")
            if A == "": wx.MessageBox("Action Canceled", "Least Common Denominator")
            elif float(int(A)) != float(A):
                wx.MessageBox("The number must be an integer", "Least Common Denominator")
            else:
                B = wx.GetTextFromUser("Enter the second number.", "Least Common Denominator")
                if B == "": wx.MessageBox("Action Canceled", "Least Common Denominator")
                elif float(int(B)) != float(B):
                    wx.MessageBox("The number must be an integer", "Least Common Denominator")
                else:
                    X = PiMath.LCD(int(A), int(B))
                    wx.MessageBox("The LCD of " + str(int(A)) + " and " + str(int(B)) + 
                                  " is " + str(X), "Least Common Denominator")
        except:
            wx.MessageBox("You must enter a number", "Least Common Denominator")

    def OnBtnIsPrimeClick(self, event):
        num = wx.GetTextFromUser("Enter a positive integer to determine if it is prime.", "Is Prime?")
        try:
            if num == "":
                wx.MessageBox("Action Canceled", "Is Prime?")
            elif float(int(num)) != float(num) or float(num) < 0:
                #If the number is a float, or not a number at all, the try
                #    statement will fail and drops to the except part.
                wx.MessageBox("The number must be a positive integer", "Is Prime?")
            elif int(num) == 0:
                wx.MessageBox("Zero is not prime")
            else:
                Num = int(num)
                X = PiMath.IsPrime(Num)
                if X[0] == False:
                    wx.MessageBox("Not Prime:\n" + str(Num) + " / " + str(X[1]) +
                                  " = \n" + str(Num/X[1]), "Is Prime?")
                else:
                    wx.MessageBox(str(Num) + " is prime", "Is Prime?")
        except:
            wx.MessageBox("The number must be a positive integer", "Is Prime?")

    def OnBtnRedRadClick(self, event):
        Radicand = wx.GetTextFromUser("Enter the radicand which you are trying to reduce",
                                      "Reduce Radical")
        try:
            if Radicand == "": wx.MessageBox("Action Canceled", "Reduce Radical")
            elif float(Radicand) >= 0.0:
                X = PiMath.ReduceRadical(float(Radicand))
                if X[1] == 1:
                    wx.MessageBox("sqrt(" + str(Radicand) + ") = \n" + str(X[0]))
                elif X[0] == 1:
                    wx.MessageBox(str(Radicand) + " Cannot Be Reduced", "Reduce Radical")
                else:
                    wx.MessageBox("sqrt(" + str(Radicand) + ") = \n" + str(X[0]) + 
                                  " sqrt(" + str(X[1]) + ")", "Reduce Radical")
            else: wx.MessageBox("The Radicand Must Be Positive", "Reduce Radical")
        except:
            wx.MessageBox("Please Input A Number", "Reduce Radical")
